<template>
  <div class="app-container">
    <el-page-header
      @back="goBack"
      :content="$t('view_details')"
      style="margin-bottom: 20px"
    >
    </el-page-header>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="auto"
      v-show="showSearch && queryParams.params"
    >
      <el-form-item
        v-for="(item, index) in searchList"
        :label="item.paramName"
        :prop="item.paramKey"
        :key="index"
      >
        <el-input
          v-model="item.value"
          :placeholder="`${$t('please_enter',{name:''})}${item.paramName}`"
          clearable
          @keyup.enter.native="handleQuery"
          @input="$forceUpdate()"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >{{ $t("redownload_payroll_report") }}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getData"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="salaryReportList" border :max-height="tableHeight">
      <el-table-column
        :label="item.paramName"
        align="center"
        :prop="item.paramKey"
        :show-overflow-tooltip="true"
        v-for="(item, index) in salaryReportDetail.salaryReportHeaderList"
        :key="index"
      >
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getData"
      :page-sizes="[10,20,30,50,100, 200]"
    />
  </div>
</template>

<script>
import { getSalaryReport } from "@/api/salary/salaryReport";
export default {
  name: "SalaryReportDetail",
  dicts: ["salary_calculation_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 薪酬报告详情
      salaryReportDetail: {},
      salaryReportList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      searchList: [],
    };
  },
  created() {
    this.getData();
  },
  methods: {
    goBack() {
      this.$router.back(-1);
    },
    getData() {
      this.loading = true;
      if (this.searchList.length) {
        this.searchList.forEach((item) => {
          this.queryParams.params[item.paramKey] = item.value;
        });
      }
      getSalaryReport({
        ...this.queryParams,
        id: this.$route.query.reportId,
      }).then((response) => {
        this.salaryReportDetail = response.data;
        this.salaryReportList = response.data.salaryReportDataList.records.map(
          (item) => {
            return {
              ...item,
              ...JSON.parse(item.dataJson),
            };
          }
        );
        this.total = response.data.salaryReportDataList.total;

        if (!this.queryParams.params) {
          const searchList =
            this.salaryReportDetail.salaryReportHeaderList.filter((item) => {
              return item.isShow == 1;
            });
          let params = {};
          searchList.forEach((item) => {
            params[item.paramKey] = null;
          });
          this.queryParams.params = params;
          this.searchList = searchList;
        }

        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.searchList.forEach((item) => {
        item.value = null;
      });
      this.$forceUpdate();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 导出按钮操作 */
    handleExport() {
      // this.download(
      //   "salary/salaryReport/export",
      //   {
      //     ...this.queryParams,
      //   },
      //   `salaryReport_${new Date().getTime()}.xlsx`
      // );
    },
  },
};
</script>
